import { GetOrderList, GetWorkOrderList } from "~/api";

export const useContent = () => {
    const keyword = ref("");
    const list = ref([]);
    const pagination = reactive({
        page: 1,
        size: 10,
    });

    const isLoading = ref(false);
    const tabActive = ref(1);
    const tabList = reactive([
        {
            label: "模板订单",
            value: 1,
        },
        {
            label: "课件订单",
            value: 2,
        },
    ]);

    const filterList = computed(() => {
        return list.value.filter((item) => {
            if (item.info) {
                let name = item.info.name;
                return name ? name.includes(keyword.value) : false;
            } else {
                return false;
            }
        });
    });

    const renderList = computed(() => {
        return filterList.value.slice(
            (pagination.page - 1) * pagination.size,
            pagination.page * pagination.size
        );
    });

    const GetList = async () => {
        const ApiFunc = {
            1: GetOrderList,
            2: GetWorkOrderList,
        }[tabActive.value];

        isLoading.value = true;
        const res = await ApiFunc();
        isLoading.value = false;

        if (res.code == 200) {
            let data = res.data;
            list.value = data;
        } else {
            Message().error(res.msg);
        }
    };

    const onSearch = (value) => {
        pagination.page = 1;
        keyword.value = value;
    };

    watch(tabActive, () => {
        list.value = [];
        pagination.page = 1;
        GetList();
    });

    return {
        isLoading,
        tabActive,
        tabList,
        filterList,
        pagination,
        renderList,
        GetList,
        onSearch,
    };
};
